package com.why.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.SecureRandom;
import java.util.UUID;

@RestController
public class AliapyController {
    private final String APP_ID = "2021000117624784";
    private final String APP_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCAQZIDW7IFwJB8wuSMUUgg17emQyEBkQYqmuHfXPhR/SDmvdkaBf1uXetJhaAa+ErjWNfl82/5MQKbI5WkgLv5B7umny9QcygurYEuBBbAnvpBHgSf0qQJ2sBbNl/idp7EIgVLv9MTUznIugiR2ywjogxd+6FmvQHUOnwN+0M8YekpyoNepm9nZI5kkGEshmTGRNBuJl7GQtWJ20kCEDGU1xR7bzEKNzfk7sgloVar3reBBvpSr/6OmaSsE9XFgchytzr9flZFPb5TC0UdaUmseR4a97hAu8XHnoULSy6VO4OwgR4dYPmcn+GfmLXeNIrjI9JgQCtig0HGW8aVREUXAgMBAAECggEAUx8r7HU/GS9F9TWyPaZoIrDr2xtihstgvbgHb2Wnoit1aVXdIFqFd0PzO4EY1KHalyF7OxLcnRnAYJ9dWnH8YVc7rK86UN9xSV81e4c237uo4Q79XD9+BMz7IaJJ3lGWb/LEj+VQfRPRPWAkMbNkFevuK/LrFdSlHNCSmdUgLD8pJvTv8KsyADkOeTH/70jMM99L2ZoTw8m9lrclavb84w8cmLVqJuMb/w81+wEWzx7jLpJybeek09/fTGGlsG1XzIx5/IFceMNWxOcn117NdXzVscFFlPLYYkASBIRf5KFHAmiTSBIW5wt181uht/ne4dIxrRS4X2UsAiexXHsHwQKBgQD6wxyXKRf449Ts8b20LC5UA2Vvmyj74yCQAZO6abrT9p1Fj3cSmYSl6hkw/qtOUShPMijJr8wIhqhYKMzBDRTiPwFar9wfnSDd0aRRHKqXwP0PHqpmi6ikWvhS2CetGwXHcQaB1Gsm74n5D0kWwArCYPj4bghO/SDVqyZmE9QfpwKBgQCC72NfZ1vLS9bO0eS25wBM3E95XJFGHTcRu1BFhTmIbE70Lt8KXiZ0y4aE5oYPZJMrp0cCYZ2CsnXaur8YLflYFLRtKlqwTBckUn+yR2qJuoefqMh4pE3vpSIvtoeXXLe7hs66as0DSOk6jZPCPy7K7up+6ls4SUnPjB/KV63dEQKBgDVjTvX74MezsgSUX0A52881PeZqEWq3a5pdS5d3pTg1nDxQFBjYqH4ufhiJT+2Hp95cnoR6MiVqjfLfFODRKS4WHfKx2Fq+uF2hoatHQCa5BueuLrdQaqI7X/07Wg8n3q4cqTcBA6Mxgp2NHJGEZ4YE4Vz7CDfQ+qmjG7TF6fanAoGAYTPwtOnZdrIllUKzJamWSnkfsvwqiT+1Gz/Q1tuRm5q0foixYlQE/p39jnpIufKzYyv7BmSLd3SfE7OhnAS9YEt2qrhPvSOv5nIshKCKEhddCgVWVmG7zhXsrDk4rgDSEbCBllklO7QMGUDzmbgoN9VoVrIK6F1YI3FmRGQVGdECgYEA6WyB+BFGBbWCtI99RyXq8FOhv60OeHxHn9acCzFKVmGfRPf1pHQyEmDjFX/BZJpQ/bhdwsGGkAFg50afHkCYB5A2FNe4Un/dFXOH5/hUEAG6Pn3DP3r136LO3fadXoC2c/RyaoU+KdwTj6/0L0YoJOJEsZq0FHin7qeHjiOP4A0=";
    private final String CHARSET = "UTF-8";
    private final String ALIPAY_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIgHnOn7LLILlKETd6BFRJ0GqgS2Y3mn1wMQmyh9zEyWlz5p1zrahRahbXAfCfSqshSNfqOmAQzSHRVjCqjsAw1jyqrXaPdKBmr90DIpIxmIyKXv4GGAkPyJ/6FTFY99uhpiq0qadD/uSzQsefWo0aTvP/65zi3eof7TcZ32oWpwIDAQAB";
    //这是沙箱接口路径,正式路径为https://openapi.alipay.com/gateway.do
    private final String GATEWAY_URL ="https://openapi.alipaydev.com/gateway.do";
    private final String FORMAT = "JSON";
    //签名方式
    private final String SIGN_TYPE = "RSA2";
    //支付宝异步通知路径,付款完毕后会异步调用本项目的方法,必须为公网地址
    private final String NOTIFY_URL = "http://127.0.0.1/notifyUrl";
    //支付宝同步通知路径,也就是当付款完毕后跳转本项目的页面,可以不是公网地址
//    private final String RETURN_URL = "http://127.0.0.1/returnUrl";

    private final String RETURN_URL ="http://localhost:919/index.html";

    @RequestMapping("/aliapy")
    public void alipay(HttpServletResponse httpResponse,String price) throws IOException {

        SecureRandom r= new SecureRandom();
        //实例化客户端,填入所需参数
        AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        //商户订单号，商户网站订单系统中唯一订单号，必填
        //生成随机Id
        String out_trade_no = UUID.randomUUID().toString();
        //在公共参数中设置回跳和通知地址
        request.setReturnUrl(RETURN_URL);
        request.setNotifyUrl(NOTIFY_URL);


        //付款金额，必填
//        String total_amount =Integer.toString(r.nextInt(9999999)+1000000);
        String total_amount=price;
        //订单名称，必填
//        String subject ="奥迪A8 2021款 A8L 60 TFSl quattro豪华型";
        String subject="购物车商品";
        //商品描述，可空
        //        String body = "尊敬的会员欢迎购买奥迪A8 2021款 A8L 60 TFSl quattro豪华型";
        String body="";
        request.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        String form = "";
        try {
            form = alipayClient.pageExecute(request).getBody(); // 调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        httpResponse.setContentType("text/html;charset=" + CHARSET);
        httpResponse.getWriter().write(form);// 直接将完整的表单html输出到页面
        httpResponse.getWriter().flush();
        httpResponse.getWriter().close();
//        return JSONArray.toJSON(form);
    }
}
